package cn.qiyu5522.tk_ems.controller.admin;


import cn.qiyu5522.tk_ems.entity.Department;
import cn.qiyu5522.tk_ems.entity.vo.DepartmentVo;
import cn.qiyu5522.tk_ems.entity.dto.QueryParamDTO;
import cn.qiyu5522.tk_ems.myanotation.WebLog;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.DepartmentService;
import cn.qiyu5522.tk_ems.utils.RedisKey;
import cn.qiyu5522.tk_ems.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-04
 */
@Slf4j
@Api(tags = "院系管理")
@RestController
@RequestMapping("/admin/department")
public class DepartmentController {
    @Resource
    private DepartmentService departmentService;
    @Resource
    private RedisUtil redisUtil;


    @WebLog(description = "分页条件查询院系列表")
    @ApiOperation("院系查询")
    @ApiImplicitParam(name = "queryParamDTO", value = "条件查询对象", required = true, dataTypeClass = QueryParamDTO.class)
    @PostMapping("/query")
    public R query(@Validated @RequestBody QueryParamDTO<DepartmentVo> queryParamDTO) {
        return departmentService.queryDepartmentByPageCondition(queryParamDTO);
    }

    @WebLog(description = "院系新增--批量")
    @ApiOperation("院系新增")
    @ApiImplicitParam(name = "departmentVos", value = "院系新增", required = true, dataTypeClass = List.class)
    @PostMapping("/add")
    public R add(@Validated @RequestBody List<DepartmentVo> departmentVos) {
        return departmentService.saveMore(departmentVos);
    }

    @WebLog(description = "院系编辑")
    @ApiOperation("院系编辑")
    @ApiImplicitParam(name = "department", value = "院系实体类对象", required = true, defaultValue = "{}", dataTypeClass = Department.class)
    @PutMapping("/edit")
    public R edit(@Validated @RequestBody Department department) {
        boolean b = departmentService.updateById(department);
        if (b) {
            // 编辑成功，刷新Redis中的缓存
            // 查询全部的院系数据
            QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("id", "name");
            List<Department> departmentList = departmentService.list(queryWrapper);
            redisUtil.set(RedisKey.DEPARTMENT_ID_AND_NAME, departmentList, RedisKey.EXPIRE_ONE_DAY);
        }
        return b ? R.ok().message("编辑成功") : R.error().message("编辑失败");
    }

    @WebLog(description = "院系删除")
    @ApiOperation("院系删除")
    @ApiImplicitParam(name = "ids", value = "院系编号集合", required = true, defaultValue = "[]", dataTypeClass = List.class)
    @PostMapping("/remove")
    public R remove(@RequestBody List<Long> ids) {
        return departmentService.removeMore(ids);
    }

    @ApiOperation("查询全部院系编号与名称")
    @GetMapping("/queryForIdAndName")
    public R queryForIdAndName() {
        List<Department> forIdAndName = departmentService.getForIdAndName();
        return R.ok().data("departmentListForIdAndName", forIdAndName);
    }

    @ApiOperation("检测院系名称是否重复")
    @ApiImplicitParam(name="name",value = "院系名称", required = true, paramType = "path", dataType = "String")
    @GetMapping("/queryForCheckRepeatName/{name}")
    public R queryForCheckRepeatName(@NotBlank(message = "根据名称查询院系时,院系名称不能为空") @PathVariable String name){
        return departmentService.queryByName(name);
    }

}
